//
//  XLSwitchUserView.m
//  industry
//
//  Created by xl on 2020/12/17.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLSwitchUserView.h"
#import "XLSwitchUserViewProtocol.h"
#import "XLSwitchUserAdapter.h"
#import "XLSwitchUserPresenterProtocol.h"
#import "XLImageAndTitleButton.h"
#import "XLBindClientAlertView.h"

@interface XLSwitchUserView ()<XLSwitchUserViewProtocol, XLAdapterDelegate, XLBindClientAlertViewDelegate>

@property (nonatomic, strong) XLTableView *tableView;
@property (nonatomic, strong) XLSwitchUserAdapter *adapter;
@property (nonatomic, strong) XLImageAndTitleButton *addBtn;
@property (nonatomic, strong) XLBindClientAlertView *addAlertView;
@property (nonatomic, strong) AUToast *toastLoad;

@end
@implementation XLSwitchUserView

- (void)dealloc {
    XLLog(@"XLSwitchUserView dealloc");
}

#pragma mark - XLSwitchUserViewProtocol
- (void)buildView {
    [self addSubview:self.addBtn];
    [self addSubview:self.tableView];
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.bottom.equalTo(self).offset(-SAFEAREA_BOTTOM_HEIGHT - 20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 30*2, 45));
    }];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.bottom.equalTo(self.addBtn.mas_top).offset(-10);
    }];
    
    XLSend(self.baseController.presenter, XLSwitchUserPresenterProtocol, requestUserList);
}

- (void)refreshTableView:(NSArray *)dataArr {
    [_adapter setAdapterArray:dataArr];
    [_tableView reloadData];
}

//正在加载中的动画
- (void)showLoadingAnimalWithTitle:(NSString *)title {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.toastLoad = [XLToast showLoadingTitle:title superView:window];
}

- (void)hideLoadingAnimal {
    if (self.toastLoad) {
        [self.toastLoad dismissToast];
    }
}

- (void)hideAddClientAlertView {
    [_addAlertView hideAllView];
    //重新请求用户列表
    XLSend(self.baseController.presenter, XLSwitchUserPresenterProtocol, requestUserList);

}

#pragma mark - XLAdapterDelegate
- (void)didSelectCellData:(id)cellData {
    XLSend(self.baseController.presenter, XLSwitchUserPresenterProtocol, handleCellData:cellData);
}

#pragma mark - XLBindClientAlertViewDelegate
- (void)clickBindWithContent:(NSString *)content {
    XLSend(self.baseController.presenter, XLSwitchUserPresenterProtocol, requestAddNewClient:content);
}

#pragma mark - UIButton 点击事件
//点击新增客户
- (void)clickAddClienter {
    [self.addAlertView showAlertView];
}

#pragma mark - Setter/getter
- (XLTableView *)tableView {
    if (!_tableView) {
        _tableView = [[XLTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor whiteBgColor];
        _tableView.delegate = self.adapter;
        _tableView.dataSource = self.adapter;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (XLSwitchUserAdapter *)adapter {
    if (!_adapter) {
        _adapter = [[XLSwitchUserAdapter alloc] init];
        _adapter.adapterDelegate = self;
    }
    return _adapter;
}

- (XLImageAndTitleButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [[XLImageAndTitleButton alloc] initWithFrame:CGRectZero imageSize:CGSizeMake(16, 16) imageAndTitleSpace:11 imageRelativeTitlePosition:XLImageRelativeTitlePositionLeft];
        _addBtn.titleLabel.font = [UIFont fontOfPingFangSCSemiboldWithSize:16];
        [_addBtn setTitleColor:[UIColor blueFontColor] forState:UIControlStateNormal];
        _addBtn.backgroundColor = [UIColor whiteColor];
        _addBtn.layer.cornerRadius = 22.5;
        _addBtn.layer.borderWidth = 1.0f;
        _addBtn.layer.borderColor = [UIColor blueBgColor].CGColor;
        _addBtn.layer.masksToBounds = YES;
        [_addBtn setTitle:@"新增绑定客户" forState:UIControlStateNormal];
        [_addBtn setImage:XLIMAGE(@"xz") forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(clickAddClienter) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

- (XLBindClientAlertView *)addAlertView {
    if (!_addAlertView) {
        _addAlertView = [[XLBindClientAlertView alloc] init];
        _addAlertView.delegate = self;
    }
    return _addAlertView;
}

@end
